home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6212 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: news.connect.net!usenet
  2. From: tomw@intelligraphics.com
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Mod or if?
  5. Date: 11 Feb 1996 22:56:05 GMT
  6. Organization: Connection Technologies
  7. Message-ID: <4fls65$a9m@dallas1.connect.net>
  8. References: <4fg3jm$fb8@gondor.sdsu.edu>
  9. Reply-To: tomw@intelligraphics.com
  10. NNTP-Posting-Host: igxtest.intelligraphics.com
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <4fg3jm$fb8@gondor.sdsu.edu>, weikel@rohan.sdsu.edu (weikel) writes:
  14. >Which is better?
  15. >
  16. >x = (x mod 7);
  17. >
  18. >or
  19. >
  20. >if (x == 7) x = 0; 
  21. >
  22. >in terms of performance?  I know that the mod function is slow
  23. >,but the if method seems brute-forceish.
  24.  
  25. My guess would be that the if statement is faster, in general.  However,
  26. with P6 and P7 optimizations the *only* way to tell *for sure* is to time it.
  27. In this particular instance, an even faster method would probably be
  28.  
  29.   x = x & 7;
  30.  
  31. This only works for values which are 2^n-1, of course.  (I'm assuming
  32. that this code is in the middle of a loop that increments or decrements x.)
  33.  
  34. Most people probably find the if statement to be a bit more readable than
  35. the modulus statement; that should also be taken into consideration.  Only
  36. optimize if it's worth the effort and potential cost in readability.
  37.  
  38. +---------------------------------------------------------------------------+
  39. + Tom Wheeler                          | Member NRA, NMRA                   +
  40. + tomw@intelligraphics.com             | OS/2 user, C++ programmer          +
  41. + ------------------------------------------------------------------------- +
  42. + Postmodernism is the refusal to think.  Deconstructionism is the refusal  +
  43. + to believe that anyone else can either.                                   +
  44. +---------------------------------------------------------------------------+
  45. + Use or reproduction of this document or the author's email address for    +
  46. + commercial purposes without the author's permission is prohibited.        +
  47. +---------------------------------------------------------------------------+
  48.  
  49.